package cn.fuxi2022.auth.controller;

import cn.fuxi2022.auth.service.TokenService;
import cn.fuxi2022.auth.service.UriService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * 网关验证
 *
 * @author fuxi
 */
@RestController
@RequestMapping("/gatewayAuthentication")
public class GatewayAuthenticationController {

    @Autowired
    private UriService uriService;

    @Autowired
    private TokenService tokenService;

    /**
     * 暴露接口：网关调用，判断当前URI和token是否放行
     *
     * @param uri   当前请求URI
     * @param token 当前请求携带的token
     * @return true：放行 false：不放行
     */
    @PostMapping("/checkToken")
    public boolean checkUriAndToken(
            @RequestParam(value = "uri", required = false) String uri,
            @RequestParam(value = "token", required = false) String token) {
        // 1.判断URI是否需要放行
        if (uriService.checkUri(uri)) {
            return true;
        }

        // 2.判断token是否合法
        return tokenService.checkToken(token);
    }
}
